import instance, { cancelAllRequest, cancelRequest } from './index'
import { RequestConfig } from '../request'
import { AxiosResponse } from 'axios';


interface YGLRequestConfig<T> extends RequestConfig {
  data?: T
}
interface YGLResponse<T> extends AxiosResponse {
  code: number
  message: string
  data: T
}

/**
 * @description: 函数的描述
 * @interface D 请求参数的interface
 * @interface T 响应结构的intercept
 * @param {YGLRequestConfig} config 不管是GET还是POST请求都使用data
 * @returns {Promise}
 */
const YGLRequest = <D, T = any>(config: YGLRequestConfig<D>) => {
  const { method = 'GET' } = config
  config.baseURL = 'https://api-hmugo-web.itheima.net'
  if (method === 'get' || method === 'GET') {
    config.params = config.data
  }
  return instance.request<YGLResponse<T>>(config)
}

YGLRequest.cancelAllRequest = cancelAllRequest
YGLRequest.cancelRequest = cancelRequest

export default YGLRequest
